package bgate.connector;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Vector;

import android.util.Log;


public class ClientConnector {

    private String _host;
    private int _port;    
    private long _userId;
    private boolean _connected;
    private Socket _socket;
    private DataTransfer _parent;
    private ConnectionHandler _connectionHandler;
    private Vector _outQueue;
    private final static String TAG="ClientConnector";

    public Socket getSocket() {
        return _socket;
    }



    public ClientConnector(String host, int port, Vector outQueue, DataTransfer parent) {
        _host = host;
        _port = port;

        _outQueue = outQueue;
        _parent = parent;
        //create socket is here
        try {
            connect();
            if (!_connected) {
                synchronized (_outQueue) {
                    _outQueue.addElement("off:5");
                    _outQueue.notifyAll();
                }
            }
            System.out.println("Connected");
        } catch (Exception ex) {
            //\\System.out.println("SlaveLocal-Error:" + ex);
            ex.printStackTrace();
        }
    }
    private Socket createNewSocket(String host,int port)
    {

    	// Create a socket with a timeout
    	try {
    	    InetAddress addr = InetAddress.getByName(host);
    	  
    	    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    	   
    	    Socket sock = new Socket();
    	    int timeoutMs = 2000;   // 2 seconds
    	    sock.connect(sockaddr, 2000);
    	    Log.d(TAG, "created socket");
    	    return sock;
    	} catch (UnknownHostException e) {
    		Log.d(TAG, e.toString());
    		return null;
    	} catch (SocketTimeoutException e) {
    		Log.d(TAG, e.toString());
    		return null;
    	} catch (IOException e) {
    		Log.d(TAG, e.toString());
    		return null;
    	}
    }

    public boolean connect(){
        try {
            if (_connected) {
                return false;
            }
            String url = "socket://" + _host + ":" + _port;
            _socket=createNewSocket(_host,_port);
            if(_socket==null)
            {
            	 _connected = false;
            	return false;
            }
            _connectionHandler = new ConnectionHandler(_socket, _parent);
            _connected = true;
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.d("go3g", "cannot connect " + ex.toString());
            System.out.println("Can not connect to server:" + ex.toString());
//            _parent.phangMidlet.destroyApp(true);
            return false;
        }
    }

    public void login(String u, String p, boolean invisible, String deliver) {
        
    }    

    public void logout() {
        
    }

    public boolean isConnected() {
        return _connected;
    }

    public void setUserId(long userId) {
        _userId = userId;
    }

    public void getContent(int appId, int groupId, int contentId, int tabID, boolean detail){
        if(_connected == true){
            //System.out.println("getContent:" + appId + "," + groupId + "," + contentId + "," + tabID);
            if(detail){
                _connectionHandler.sendMessage("GTCNT:" + appId + "," + groupId + "," + contentId + "," + tabID);
            }else{
                _connectionHandler.sendMessage("GTSUM:" + appId + "," + groupId + "," + contentId + "," + tabID);
            }
        }else{
            System.out.println("not connected.");
        }
    }

    public void getVas(int appId, int tabID){
        if(_connected == true){
            _connectionHandler.sendMessage("GTVAS:" + appId + "," + tabID);
        }else{
            System.out.println("not connected.");
        }
    }

    public void getGroups(int appId){        
        if(_connected == true){            
            _connectionHandler.sendMessage("GTGRP:" + appId);
        }else{
            System.out.println("not connected.");
        }
    }

    public void getItems(int appId, int groupId, int page){
        if(_connected == true){
            _connectionHandler.sendMessage("GITEM:" + appId + "," + groupId + "," + page);
        }else{
            System.out.println("not connected.");
        }
    }


    public void requestData(String type){
        System.out.println("client request "+type);
        if(_connected == true){
            _connectionHandler.sendMessage(type);
        }else{
            System.out.println("not connected.");
        }
    }
    

}
